package com.android.systemui.ios.notice;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.android.systemui.ios.R;

import java.text.SimpleDateFormat;
import java.util.List;

public class NoticeAdapter extends RecyclerView.Adapter<NoticeHolder> {

    private final Context mContext;
    private final List<NoticeData> mList;
    private final SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");

    public NoticeAdapter(Context context, List<NoticeData> dataList) {
        mContext = context;
        mList = dataList;
    }

    @Override
    public NoticeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        itemView = LayoutInflater.from(mContext).inflate(R.layout.notice_item_layout, parent, false);
        return new NoticeHolder(viewType, itemView);
    }

    @Override
    public void onBindViewHolder(NoticeHolder holder, int position) {
        NoticeData noticeData = mList.get(position);
        if (noticeData.getTitle() != null) {
            holder.mTitle.setText(noticeData.getTitle());
        }
        if (noticeData.getSummary() != null) {
            holder.mSummary.setText(noticeData.getSummary());
        }
        if (noticeData.getWhen() != 0) {
            holder.mTime.setText(mDateFormat.format(noticeData.getWhen()));
        }
        if (noticeData.getIcon() != null) {
            holder.mIcon.setImageDrawable(noticeData.getIcon());
        }
        holder.itemView.setTag(noticeData.getKey());
        holder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Object obj = v.getTag();
                if (obj instanceof String) {
                    onNoticeClicked((String) obj);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getViewMode();
    }

    public void onNoticeClicked(String key) {
    }
}
